import { Handlers, PageProps } from "$fresh/server.ts";
import { DeleteCredit } from "~/denokv/resume.ts";
import { CredentialMeta, GetCreditList } from "~/denokv/resume.ts";
import Resumetable from "~/islands/resume/resumetable.tsx";
export const handler: Handlers<unknown, CredentialMeta[]> = {
  async GET(_, ctx) {
    const list = await GetCreditList();
    return ctx.render(list);
  },
  async POST(req) {
    const form = await req.formData();
    // todo:激活功能
    // form.get("action")

    const ids = form.getAll("id") as Array<string>;
    await DeleteCredit(ids);
    return new Response(null, {
      status: 302,
      headers: {
        location: req.url,
      },
    });
  },
  DELETE(req) {
    console.log("delete");
    return new Response(null, {
      status: 302,
      headers: {
        location: req.url,
      },
    });
  },
};
export default (({ url, data }: PageProps<CredentialMeta[]>) => {
  return (
    <form action={url.pathname} method="POST" class="h-full">
      <Resumetable action={url.pathname} data={data}></Resumetable>
    </form>
  );
});
